#include "head.h"

int main(int argc,char *argv[]){
    int socket_fd = socket(AF_INET,SOCK_STREAM,0);

    char *ip = "192.168.106.130";
    char *port = "8080";

    //read_log_file(ip,port);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(ip);
    addr.sin_port = htons(atoi(port));
    connect(socket_fd,(struct sockaddr *)&addr,sizeof(addr));

    // 登录信息的结构体
    client_status_t client_stat;
    memset(&client_stat, 0, sizeof(client_status_t));
    client_stat.socket_fd = socket_fd;

    while(1){

        memset(client_stat.runtime_cmd, 0, sizeof(client_stat.runtime_cmd));
        
        if(client_stat.login_status == 0){
            // 没有登录: 要求登录或者注册
            printf("[A]:登录 [B]:注册 [C]退出\n");
            char option;
            scanf("%c",&option);

            switch(option){
            case 'A':{
                         do_login(&client_stat );
                         break;
                     }
            case 'B':{
                         do_register(&client_stat);
                         break;
                     }
            case 'C':{
                         exit(0);
                     }
            }

            continue;
        }

        printf("%s:%s $  ", client_stat.user ,client_stat.path);
        fflush(stdout);


        // 记录准备要运行的命令
        char wait_run_cmd[60] = {0};
        // 读取命令
        read(STDIN_FILENO,wait_run_cmd,sizeof(wait_run_cmd));

        // 移除换行
        wait_run_cmd[strlen(wait_run_cmd)-1] = 0;

        // 把命令赋值给客户端的状态结构体
        memcpy(client_stat.runtime_cmd, wait_run_cmd, strlen(wait_run_cmd) );


        if(!strncmp(wait_run_cmd,"cd",2)){ // cd命令
            do_cd(&client_stat);
            fflush(stdout);
        }else if(!strncmp(wait_run_cmd,"ls",2)){ // ls命令
            do_ls(&client_stat);
        }else if(!strncmp(wait_run_cmd,"upload",6)){ // upload命令
            do_upload(&client_stat);
        }else if(!strncmp(wait_run_cmd,"download",8)){ // download命令
            do_download(&client_stat);
        }else if(!strncmp(wait_run_cmd,"rm",2)){ // rm命令
            do_rm(&client_stat);
            fflush(stdout);
        }else if(!strncmp(wait_run_cmd,"pwd",3)){ // pwd命令
            do_pwd(&client_stat);
        }else if(!strncmp(wait_run_cmd,"mkdir",5)){ // create_dir命令
            do_mkdir(&client_stat);
        }else{
            printf("输入的命令有误!请重新输入\n");
            continue;
        }

    }
    close(socket_fd);
    return 0;
}

